package com.blackcat.recode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackcat.recode.common.result.AjaxResult;
import com.blackcat.recode.dao.BranchMapper;
import com.blackcat.recode.dao.DeployRecodeMapper;
import com.blackcat.recode.dao.ServiceMapper;
import com.blackcat.recode.dao.UpdateRecodeMapper;
import com.blackcat.recode.entity.*;
import com.blackcat.recode.service.ServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blackcat.recode.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p> 服务信息表 服务实现类
 * @author blackcat 2024-09-10
 */
@Service
public class ServiceServiceImpl extends ServiceImpl<ServiceMapper, ServiceInfo> implements ServiceService {

    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private UpdateRecodeMapper updateRecodeMapper;
    @Autowired
    private DeployRecodeMapper deployRecodeMapper;

    @Override
    public AjaxResult remove(String id) {
        serviceMapper.deleteById(id);
        QueryWrapper<Branch> branchQueryWrapper = new QueryWrapper<>();
        branchQueryWrapper.eq("service_id", id);
        branchMapper.delete(branchQueryWrapper);
        QueryWrapper<UpdateRecode> updateRecodeQueryWrapper = new QueryWrapper<>();
        updateRecodeQueryWrapper.eq("service_id", id);
        updateRecodeMapper.delete(updateRecodeQueryWrapper);
        QueryWrapper<DeployRecode> deployRecodeQueryWrapper = new QueryWrapper<>();
        deployRecodeQueryWrapper.inSql("update_id", "select id from update_recode where service_id='"+id+"'");
        deployRecodeMapper.delete(deployRecodeQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getServiceNameList(String projectId) {
        QueryWrapper<ServiceInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        queryWrapper.select("id", "service_name");
        queryWrapper.orderByAsc("service_name");
        return AjaxResult.success(serviceMapper.selectList(queryWrapper));
    }

    @Override
    public AjaxResult getList(Integer pageNow, Integer pageSize, String serviceName, String serviceType, String projectId) {
        QueryWrapper<ServiceInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(serviceName)) {
            queryWrapper.like("service_name", serviceName);
        }
        if (StringUtils.isNotBlank(serviceType)) {
            queryWrapper.like("service_type", serviceType);
        }
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        queryWrapper.orderByAsc("service_name");
        Page<ServiceInfo> page = new Page<>(pageNow, pageSize);
        Page<ServiceInfo> result = serviceMapper.selectPage(page, queryWrapper);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult edit(ServiceInfo service) {
        if (service.getId() == null) {
            serviceMapper.insert(service);
        }else {
            serviceMapper.updateById(service);
        }
        return AjaxResult.success();
    }
}
